<?php

use PddWechat\Library\WechatLibrary;
use PddWechat\Models\Share\Data\ShareQrData;
use PddWechat\Models\User\Data\UserData;
use PddWechat\Models\User\Data\UserInviteRecordData;
use PddWechat\Models\User\DataObject\UserDo;

require_once "./base.php";

//$openId = "ofulZ02VkshM8a5Jm-4fFIg9H5QA";
//$x = "qrscene_xofulZ02VkshM8a5Jm-4fFIg9H5QA";
//
//$userData = new UserData();
//$userDo = $userData->getUserByOpenId($openId);
//
//$status = 0; //默认没有被别人邀请
//if( strpos(trim($x), "qrscene_") !== false) {
//    $inviteOpenId = str_replace("qrscene_", "", trim($x));
//}else {
//    $inviteOpenId = "";
//}
//
//var_dump($inviteOpenId);
//
//if($userDo == null) {
//    $basicUserInfo = WechatLibrary::getBasicSubscribeUserInfo($openId);
//    $userData->insert( $basicUserInfo, 1,  $inviteOpenId = str_replace("qrscene_", "", $inviteOpenId));
//    $status = 1;
//}else {
//    $userDoUpdate = new UserDo();
//    $userDoUpdate->subscribe = 1;
//    $userDoUpdate->id = $userDo->id;
//    $userData->edit($userDoUpdate);
//
//}
//
//if(! empty($inviteOpenId)) {
//    $userInviteRecordData = new UserInviteRecordData();
//    $userInviteRecordData->insertRecord($openId, $inviteOpenId, $status );
//}

//$userInviteRecordData = new UserInviteRecordData();
//$records = $userInviteRecordData ->selectOpenIdInviteRecords("ofulZ02VkshM8a5Jm-4fFIg9H5QA");
//if( count($records) == 0) {
//    return $res;
//}
//
//var_dump($records);
//
//if( count($records) > 0 ){
//    foreach ($records as $record) {
//        $openIds[] = $record->openId;
//    }
//}
//
//$userData = new UserData();
//$userList = $userData->getUserByOpenIds($openIds);
//
//var_dump($userList);
//
//foreach ($records as $record) {
//    if( isset($userList[$record->openId]) ) {
//        $record->userName = $userList[$record->openId]->userName;
//        $res['list'][] = $record;
//    }
//}
//var_dump($res);

//$cookieSuccess = APP_PATH."/var/ddjb.tmp";
//
//
//
////普通数组也行
//$url = "http://jinbao.pinduoduo.com/network/api/account/loginPassword";
//
//$data = [
//    'mobile' => "15658177153",
//    'password' => "124567as",
//];
//$dataString =  json_encode($data);
//
//
//$refer = "http://jinbao.pinduoduo.com";
////$res = call($url, $dataString, $headers, 10,  $refer, null, $cookieSuccess);
////var_dump($res);
//
//$url = "http://jinbao.pinduoduo.com/network/api/promotion/redPacket/createUrl";
//$data = [
//    'customParameters' => "tttt",
//    'pid' => "1884871_22329699",
//];
//$dataString =  json_encode($data);
//
//$headers = [
//    'Content-Type: application/json',
//    'Content-Length: ' . strlen($dataString),
//    'Cookie: api_uid=rBQ5sVq6cR6xfEqJBBhHAg==; DDJB_PASS_ID=caefa65317b1832ad3e73bd24a2b77e4'
//];
//
//$res = call($url, $dataString, $headers, 10,  $refer, null, null);
//var_dump($res);
//
//
//
//function call($url, $post = null, $headers =null, $timeout = 10, $refer=null, $cookie = null, $withCookie = null)
//{
//    $curl = curl_init();
//    curl_setopt($curl, CURLOPT_URL, $url);
//echo "$url";
//    curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
//    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
//    curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
//
//    if( $headers) {
//        curl_setopt( $curl, CURLOPT_HTTPHEADER, $headers );
//    var_dump($headers);
//    }
//
//    if ($post) {
//        var_dump($post);
//        curl_setopt($curl, CURLOPT_POST, 1);
//        curl_setopt($curl, CURLOPT_POSTFIELDS, $post);
//    }
//    if ($cookie) {
//        curl_setopt($curl, CURLOPT_COOKIEFILE, "/Users/liujun/code/wechat/src/..//var/ddjb.tmp");
//        curl_setopt($curl, CURLOPT_COOKIEJAR, "/Users/liujun/code/wechat/src/..//var/ddjb.tmp");
//    }
//    if($refer) {
//        curl_setopt($curl, CURLOPT_REFERER, $refer);
//    }
//
//    if( trim($withCookie) != "") {
//        curl_setopt($curl, CURLOPT_COOKIEJAR, $withCookie);//用来存放登录成功的cookie
//    }
//
//    curl_setopt($curl, CURLOPT_TIMEOUT, $timeout);
//    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
//    $data = curl_exec($curl);
//    if (curl_errno($curl)) {
//        return curl_error($curl);
//    }
//    curl_close($curl);
//    return $data;
//}


//\PddWechat\Library\PddLibrary::login();
$t = \PddWechat\Library\PddLibrary::addMallUrl("111", "aaa");
echo $t."\n";

$y = \PddWechat\Library\PddLibrary::addRedPackUrl("111", "aaa");
echo $y."\n";


$useData = new \PddWechat\Models\User\Data\UserData();
$users = $useData->getList(0, 1000);
foreach ($users as $user){
    /** @var \PddWechat\Models\User\DataObject\UserDo $user */
    var_dump($user);


    if( trim($user->inviteCode) == "") {
        $user->inviteCode = \PddWechat\Library\InviteCodeLibrary::createCode();
        $useData->edit($user);
    }

    $userPromotionUrlDao = new \PddWechat\Models\User\Dao\UserPromotionUrlDao();
    $userDo = new \PddWechat\Models\User\DataObject\UserPromotionUrlDo();
    $userDo->openId = $user->openId;
    $userDo->mallUrl = \PddWechat\Library\PddLibrary::addMallUrl($user->openId, $user->inviteCode);
    $userDo->redPackUrl = \PddWechat\Library\PddLibrary::addRedPackUrl($user->openId, $user->inviteCode);
    $userPromotionUrlDao->insert($userDo);
    sleep(5);
}